﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Markup;
using Microsoft.Win32;
using Microsoft.Surface.Presentation;
using Microsoft.Surface.Presentation.Controls;
using Microsoft.Surface.Presentation.Input;
using System.Xml;
using System.Globalization;
using System.IO;
using System.ComponentModel;
using Ab2d.Common.ReaderWmf;

namespace MultiTouchDemonstrator
{
    class Symbol 
    {
        Canvas map = null;
        private SymbolManipulation symbolManipulation = null;
        void img_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            if (symbolManipulation == null)
            {
                symbolManipulation = new SymbolManipulation();
            }
            symbolManipulation.ManipulationCompleted(sender, e, map);

        }

        void img_ManipulationInertiaStarting(object sender, ManipulationInertiaStartingEventArgs e)
        {
            if (symbolManipulation == null)
            {
                symbolManipulation = new SymbolManipulation();
            }
            symbolManipulation.ManipulationInertiaStarting(sender, e, 0.2, 0, true);


        }

        void img_ManipulationStarting(object sender, ManipulationStartingEventArgs e)
        {
            if (symbolManipulation == null)
            {
                symbolManipulation = new SymbolManipulation();
            }
            symbolManipulation.ManipulationStarting(sender, e, map, true);

        }

        void img_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            if (symbolManipulation == null)
            {
                symbolManipulation = new SymbolManipulation();
            }
            symbolManipulation.ManipulationDelta(map, e);

        }
        public void AddSymbol(Canvas container, LibraryContainer libraryContainer, RoutedPropertyChangedEventArgs<double> zoomSlider, Point zoom, string path, double offsetX, double offsetY, bool drop)
        {
            Image img = new Image();
            img.Source = new BitmapImage(new Uri(path, UriKind.Absolute));
            Point originalImageSource = new Point(img.Source.Width, img.Source.Height);
            img.Tag = originalImageSource;
            map = container;
            map.Children.Add(img);
            img.IsManipulationEnabled = true;
            img.ManipulationCompleted += new EventHandler<ManipulationCompletedEventArgs>(img_ManipulationCompleted);
            img.ManipulationDelta += new EventHandler<ManipulationDeltaEventArgs>(img_ManipulationDelta);
            img.ManipulationStarting += new EventHandler<ManipulationStartingEventArgs>(img_ManipulationStarting);
            img.ManipulationInertiaStarting += new EventHandler<ManipulationInertiaStartingEventArgs>(img_ManipulationInertiaStarting);
            img.Width = zoomSlider.OldValue * img.Source.Width / 100;
            img.Height = zoomSlider.OldValue * img.Source.Height / 100;

            img.Width = img.Width / zoom.X;
            img.Height = img.Height / zoom.Y;

            if (zoom.X < 3 || zoom.Y < 3)
            {
                img.Visibility = Visibility.Hidden;
            }
            else
            {
                img.Visibility = Visibility.Visible;
            }
            TranslateTransform translation = new TranslateTransform(offsetX, offsetY);
            RotateTransform rotation = new RotateTransform(0);
            TransformGroup group = new TransformGroup();
            group.Children.Add(translation);
            group.Children.Add(rotation);
            img.RenderTransform = group;
            if (drop)
            {
                foreach (var node in libraryContainer.ItemsSource)
                {
                    libraryContainer.SetIsItemDataEnabled(node, true);

                }
            }
        }
    }
}
